 #include "types.h"
#include "ms_rwreg.h"
#include "ms_reg.h"
#include "autogamma.h"
#include "NVRam.h"
#include "msflash.h"

extern void printData( char *str, WORD value );

#if ENABLE_DeltaE

extern XDATA BYTE* tAllFGamma3x3Matrix[2];

#if CHIP_ID == CHIP_TSUMF
code BYTE tRolexSRGBDeGamma[] =
{
0x00, 0x00, 0x14, 0x00, 0x28, 0x00, 0x3B, 0x00,
0x4F, 0x00, 0x63, 0x00, 0x77, 0x00, 0x8C, 0x00,
0xA0, 0x00, 0xB3, 0x00, 0xC7, 0x00, 0xDB, 0x00,
0xF1, 0x00, 0x08, 0x01, 0x20, 0x01, 0x39, 0x01,
0x53, 0x01, 0x6F, 0x01, 0x8B, 0x01, 0xAB, 0x01,
0xCA, 0x01, 0xEB, 0x01, 0x0D, 0x02, 0x31, 0x02,
0x55, 0x02, 0x7C, 0x02, 0xA4, 0x02, 0xCD, 0x02,
0xF8, 0x02, 0x24, 0x03, 0x51, 0x03, 0x80, 0x03,
0xB1, 0x03, 0xE3, 0x03, 0x16, 0x04, 0x4B, 0x04,
0x81, 0x04, 0xBA, 0x04, 0xF3, 0x04, 0x2E, 0x05,
0x6B, 0x05, 0xA9, 0x05, 0xEA, 0x05, 0x2B, 0x06,
0x6E, 0x06, 0xB3, 0x06, 0xFA, 0x06, 0x42, 0x07,
0x8C, 0x07, 0xD8, 0x07, 0x25, 0x08, 0x73, 0x08,
0xC4, 0x08, 0x17, 0x09, 0x6B, 0x09, 0xC0, 0x09,
0x19, 0x0A, 0x72, 0x0A, 0xCD, 0x0A, 0x2A, 0x0B,
0x89, 0x0B, 0xE9, 0x0B, 0x4C, 0x0C, 0xB0, 0x0C,
0x16, 0x0D, 0x7E, 0x0D, 0xE7, 0x0D, 0x53, 0x0E,
0xC1, 0x0E, 0x31, 0x0F, 0xA1, 0x0F, 0x14, 0x10,
0x8A, 0x10, 0x00, 0x11, 0x7A, 0x11, 0xF4, 0x11,
0x72, 0x12, 0xF0, 0x12, 0x71, 0x13,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x00,
0x32, 0x00, 0x3E, 0x00, 0x4A, 0x00, 0x56, 0x00,
0x62, 0x00, 0x6E, 0x00, 0x7A, 0x00, 0x86, 0x00,
0x92, 0x00, 0x9E, 0x00, 0xAA, 0x00, 0xB6, 0x00,
0xC2, 0x00, 0xCE, 0x00, 0xDA, 0x00, 0xE6, 0x00,
0xF2, 0x00, 0x0E, 0x01, 0x2A, 0x01, 0x5A, 0x01,
0x69, 0x01, 0x8B, 0x01, 0xAE, 0x01, 0xD3, 0x01,
0xFA, 0x01, 0x23, 0x02, 0x4E, 0x02, 0x7A, 0x02,
0xA7, 0x02, 0xD7, 0x02, 0x08, 0x03, 0x3B, 0x03,
0x71, 0x03, 0xA7, 0x03, 0xE0, 0x03, 0x1A, 0x04,
0x56, 0x04, 0x94, 0x04, 0xD4, 0x04, 0x15, 0x05,
0x59, 0x05, 0x9F, 0x05, 0xE6, 0x05, 0x2F, 0x06,
0x7A, 0x06, 0xC7, 0x06, 0x16, 0x07, 0x66, 0x07,
0xB9, 0x07, 0x0E, 0x08, 0x64, 0x08, 0xBC, 0x08,
0x17, 0x09, 0x74, 0x09, 0xD2, 0x09, 0x33, 0x0A,
0x95, 0x0A, 0xF8, 0x0A, 0x5F, 0x0B, 0xC7, 0x0B,
0x31, 0x0C, 0x9E, 0x0C, 0x0C, 0x0D, 0x7D, 0x0D,
0xEF, 0x0D, 0x63, 0x0E, 0xDA, 0x0E, 0x52, 0x0F,
0xCD, 0x0F, 0x49, 0x10, 0xC8, 0x10, 0x49, 0x11,
0xCC, 0x11, 0x50, 0x12, 0xD7, 0x12, 0x61, 0x13,
0xEB, 0x13, 0x79, 0x14, 0x08, 0x15, 0x9A, 0x15,
0x2E, 0x16, 0xC3, 0x16, 0x5B, 0x17, 0xF5, 0x17,
0x91, 0x18, 0x2F, 0x19, 0xD0, 0x19, 0x72, 0x1A,
0x17, 0x1B, 0xBE, 0x1B, 0x67, 0x1C, 0x12, 0x1D,
0xBF, 0x1D, 0x6F, 0x1E,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x11, 0x69, 0x6F, 0x11,
0x75, 0x7B, 0x11, 0x81, 0x87, 0x11,
0x8D, 0x92, 0x11, 0x98, 0x9D, 0x11,
0xA2, 0xA8, 0x11, 0xAD, 0xB2, 0x11,
0xB7, 0xBC, 0x11, 0xC1, 0xC6, 0x11,
0xCB, 0xCF, 0x11, 0xD4, 0xD9, 0x11,
0xDD, 0xE2, 0x11, 0xE6, 0xEA, 0x11,
0xEF, 0xF3, 0x11, 0xF7, 0xFC, 0x11,
0xFF, 0x03, 0x21, 0x07, 0x0B, 0x22,
0x0F, 0x13, 0x22, 0x17, 0x1B, 0x22,
0x1F, 0x23, 0x22, 0x27, 0x2A, 0x22,
0x2E, 0x32, 0x22, 0x36, 0x39, 0x22,
0x3D, 0x40, 0x22, 0x44, 0x48, 0x22,
0x4B, 0x4F, 0x22, 0x52, 0x56, 0x22,
0x59, 0x5C, 0x22, 0x60, 0x63, 0x22,
0x67, 0x6A, 0x22, 0x6D, 0x71, 0x22,
0x74, 0x77, 0x22, 0x7A, 0x7D, 0x22,
0x81, 0x84, 0x22, 0x87, 0x8A, 0x22,
0x8D, 0x90, 0x22, 0x93, 0x96, 0x22,
0x99, 0x9D, 0x22, 0xA0, 0xA3, 0x22,
0xA5, 0xA8, 0x22, 0xAB, 0xAE, 0x22,
0xB1, 0xB4, 0x22, 0xB7, 0xBA, 0x22,
0xBD, 0xC0, 0x22, 0xC2, 0xC5, 0x22,
0xC8, 0xCB, 0x22, 0xCE, 0xD0, 0x22,
0xD3, 0xD6, 0x22, 0xD9, 0xDB, 0x22,
0xDE, 0xE1, 0x22, 0xE3, 0xE6, 0x22,
0xE9, 0xEB, 0x22, 0xEE, 0xF1, 0x22,
0xF3, 0xF6, 0x22, 0xF8, 0xFB, 0x22,
0xFE, 0x00, 0x32, 0x03, 0x05, 0x33,
0x08, 0x0A, 0x33, 0x0D, 0x0F, 0x33,
0x12, 0x14, 0x33, 0x17, 0x19, 0x33,
0x1C, 0x1E, 0x33, 0x21, 0x23, 0x33,
0x25, 0x28, 0x33, 0x2A, 0x2D, 0x33,
0x2F, 0x31, 0x33, 0x34, 0x36, 0x33,
0x39, 0x3B, 0x33, 0x3D, 0x40, 0x33,
0x42, 0x44, 0x33, 0x46, 0x49, 0x33,
0x4B, 0x4D, 0x33, 0x50, 0x52, 0x33,
0x54, 0x56, 0x33, 0x59, 0x5B, 0x33,
0x5D, 0x5F, 0x33, 0x62, 0x64, 0x33,
0x66, 0x68, 0x33, 0x6A, 0x6D, 0x33,
0x6F, 0x71, 0x33, 0x73, 0x75, 0x33,
0x77, 0x7A, 0x33, 0x7C, 0x7E, 0x33,
0x80, 0x82, 0x33, 0x84, 0x86, 0x33,
0x88, 0x8B, 0x33, 0x8D, 0x8F, 0x33,
0x91, 0x93, 0x33, 0x95, 0x97, 0x33,
0x99, 0x9B, 0x33, 0x9D, 0x9F, 0x33,
0xA1, 0xA3, 0x33, 0xA5, 0xA7, 0x33,
0xA9, 0xAB, 0x33, 0xAD, 0xAF, 0x33,
0xB1, 0xB3, 0x33, 0xB5, 0xB7, 0x33,
0xB9, 0xBB, 0x33, 0xBD, 0xBF, 0x33,
0xC1, 0xC3, 0x33, 0xC5, 0xC7, 0x33,
0xC9, 0xCB, 0x33, 0xCD, 0xCF, 0x33,
0xD1, 0xD3, 0x33, 0xD4, 0xD6, 0x33,
0xD8, 0xDA, 0x33, 0xDC, 0xDE, 0x33,
0xE0, 0xE2, 0x33, 0xE4, 0xE5, 0x33,
0xE7, 0xE9, 0x33, 0xEB, 0xED, 0x33,
0xEF, 0xF1, 0x33, 0xF2, 0xF4, 0x33,
0xF6, 0xF8, 0x33, 0xFA, 0xFC, 0x33,
};

code BYTE tRolexSRGBFixGamma[] =
{
0x00, 0x32, 0x00, 0x56, 0x70, 0x00,
0x86, 0x98, 0x00, 0xA9, 0xB8, 0x00,
0xC6, 0xD2, 0x00, 0xDE, 0xEA, 0x00,
0xF5, 0xFF, 0x00, 0x09, 0x12, 0x11,
0x1B, 0x24, 0x11, 0x2C, 0x34, 0x11,
0x3C, 0x43, 0x11, 0x4B, 0x52, 0x11,
0x59, 0x60, 0x11, 0x67, 0x6E, 0x11,
0x74, 0x7A, 0x11, 0x81, 0x87, 0x11,
0x8D, 0x92, 0x11, 0x98, 0x9E, 0x11,
0xA3, 0xA9, 0x11, 0xAE, 0xB4, 0x11,
0xB9, 0xBE, 0x11, 0xC3, 0xC8, 0x11,
0xCD, 0xD2, 0x11, 0xD6, 0xDB, 0x11,
0xE0, 0xE5, 0x11, 0xE9, 0xEE, 0x11,
0xF2, 0xF7, 0x11, 0xFB, 0xFF, 0x11,
0x04, 0x08, 0x22, 0x0C, 0x10, 0x22,
0x14, 0x18, 0x22, 0x1C, 0x20, 0x22,
0x24, 0x28, 0x22, 0x2C, 0x30, 0x22,
0x34, 0x38, 0x22, 0x3B, 0x3F, 0x22,
0x43, 0x46, 0x22, 0x4A, 0x4D, 0x22,
0x51, 0x55, 0x22, 0x58, 0x5C, 0x22,
0x5F, 0x63, 0x22, 0x66, 0x69, 0x22,
0x6D, 0x70, 0x22, 0x73, 0x77, 0x22,
0x7A, 0x7D, 0x22, 0x81, 0x84, 0x22,
0x87, 0x8A, 0x22, 0x8D, 0x90, 0x22,
0x94, 0x97, 0x22, 0x9A, 0x9D, 0x22,
0xA0, 0xA3, 0x22, 0xA6, 0xA9, 0x22,
0xAC, 0xAF, 0x22, 0xB2, 0xB5, 0x22,
0xB8, 0xBB, 0x22, 0xBD, 0xC0, 0x22,
0xC3, 0xC6, 0x22, 0xC9, 0xCC, 0x22,
0xCE, 0xD1, 0x22, 0xD4, 0xD7, 0x22,
0xDA, 0xDC, 0x22, 0xDF, 0xE1, 0x22,
0xE4, 0xE7, 0x22, 0xE9, 0xEC, 0x22,
0xEF, 0xF1, 0x22, 0xF4, 0xF7, 0x22,
0xF9, 0xFC, 0x22, 0xFE, 0x01, 0x32,
0x03, 0x06, 0x33, 0x08, 0x0B, 0x33,
0x0D, 0x10, 0x33, 0x12, 0x15, 0x33,
0x17, 0x1A, 0x33, 0x1C, 0x1F, 0x33,
0x21, 0x24, 0x33, 0x26, 0x28, 0x33,
0x2B, 0x2D, 0x33, 0x30, 0x32, 0x33,
0x34, 0x37, 0x33, 0x39, 0x3B, 0x33,
0x3E, 0x40, 0x33, 0x42, 0x44, 0x33,
0x47, 0x49, 0x33, 0x4B, 0x4E, 0x33,
0x50, 0x52, 0x33, 0x54, 0x56, 0x33,
0x59, 0x5B, 0x33, 0x5D, 0x5F, 0x33,
0x61, 0x64, 0x33, 0x66, 0x68, 0x33,
0x6A, 0x6C, 0x33, 0x6E, 0x70, 0x33,
0x73, 0x75, 0x33, 0x77, 0x79, 0x33,
0x7B, 0x7D, 0x33, 0x7F, 0x81, 0x33,
0x83, 0x85, 0x33, 0x87, 0x8A, 0x33,
0x8C, 0x8E, 0x33, 0x90, 0x92, 0x33,
0x94, 0x96, 0x33, 0x98, 0x9A, 0x33,
0x9C, 0x9E, 0x33, 0xA0, 0xA2, 0x33,
0xA4, 0xA6, 0x33, 0xA8, 0xAA, 0x33,
0xAC, 0xAE, 0x33, 0xB0, 0xB2, 0x33,
0xB4, 0xB5, 0x33, 0xB7, 0xB9, 0x33,
0xBB, 0xBD, 0x33, 0xBF, 0xC1, 0x33,
0xC3, 0xC5, 0x33, 0xC7, 0xC9, 0x33,
0xCA, 0xCC, 0x33, 0xCE, 0xD0, 0x33,
0xD2, 0xD4, 0x33, 0xD6, 0xD7, 0x33,
0xD9, 0xDB, 0x33, 0xDD, 0xDF, 0x33,
0xE1, 0xE2, 0x33, 0xE4, 0xE6, 0x33,
0xE8, 0xEA, 0x33, 0xEC, 0xED, 0x33,
0xEF, 0xF1, 0x33, 0xF3, 0xF4, 0x33,
0xF6, 0xF8, 0x33, 0xFA, 0xFC, 0x33,
};

code BYTE* tAllxvYccFixGammaTab[] =
{
  tRolexSRGBFixGamma, tRolexAdobeFixGamma
};

#else
code BYTE tFGamma[] =
{
 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
 0x02, 0x03, 0x00, 0x04, 0x06, 0x00,
 0x08, 0x09, 0x00, 0x0c, 0x0e, 0x00,
 0x10, 0x13, 0x00, 0x16, 0x18, 0x00,
 0x1c, 0x1f, 0x00, 0x22, 0x26, 0x00,
 0x29, 0x2d, 0x00, 0x31, 0x35, 0x00,
 0x3a, 0x3e, 0x00, 0x43, 0x47, 0x00,
 0x4c, 0x51, 0x00, 0x56, 0x5c, 0x00,
 0x61, 0x67, 0x00, 0x6c, 0x72, 0x00,
 0x78, 0x7e, 0x00, 0x85, 0x8b, 0x00,
 0x91, 0x98, 0x00, 0x9f, 0xa6, 0x00,
 0xad, 0xb4, 0x00, 0xbb, 0xc3, 0x00,
 0xca, 0xd2, 0x00, 0xda, 0xe1, 0x00,
 0xea, 0xf2, 0x00, 0xfa, 0x02, 0x10,
 0x0b, 0x14, 0x11, 0x1c, 0x25, 0x11,
 0x2e, 0x37, 0x11, 0x41, 0x4a, 0x11,
 0x54, 0x5d, 0x11, 0x67, 0x71, 0x11,
 0x7b, 0x85, 0x11, 0x8f, 0x99, 0x11,
 0xa4, 0xae, 0x11, 0xb9, 0xc4, 0x11,
 0xcf, 0xda, 0x11, 0xe5, 0xf0, 0x11,
 0xfc, 0x07, 0x21, 0x13, 0x1f, 0x22,
 0x2a, 0x36, 0x22, 0x42, 0x4f, 0x22,
 0x5b, 0x67, 0x22, 0x74, 0x80, 0x22,
 0x8d, 0x9a, 0x22, 0xa7, 0xb4, 0x22,
 0xc1, 0xce, 0x22, 0xdc, 0xe9, 0x22,
 0xf7, 0x05, 0x32, 0x12, 0x20, 0x33,
 0x2e, 0x3d, 0x33, 0x4b, 0x59, 0x33,
 0x68, 0x76, 0x33, 0x85, 0x94, 0x33,
 0xa3, 0xb2, 0x33, 0xc1, 0xd0, 0x33,
 0xdf, 0xef, 0x33, 0xfe, 0x0e, 0x43,
 0x1e, 0x2e, 0x44, 0x3e, 0x4e, 0x44,
 0x5e, 0x6e, 0x44, 0x7f, 0x8f, 0x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
 0x36, 0x53, 0xff, 0x6f, 0x8c, 0xff,
 0xa8, 0xc5, 0xff, 0xe2, 0xff, 0xff,

 /* -2.2
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 0x01, 0x01, 0x00, 0x02, 0x03, 0x00,
 0x04, 0x05, 0x00, 0x06, 0x07, 0x00,
 0x09, 0x0a, 0x00, 0x0c, 0x0e, 0x00,
 0x10, 0x12, 0x00, 0x14, 0x16, 0x00,
 0x19, 0x1b, 0x00, 0x1e, 0x21, 0x00,
 0x24, 0x27, 0x00, 0x2a, 0x2d, 0x00,
 0x31, 0x34, 0x00, 0x38, 0x3c, 0x00,
 0x40, 0x44, 0x00, 0x48, 0x4d, 0x00,
 0x51, 0x56, 0x00, 0x5a, 0x5f, 0x00,
 0x64, 0x69, 0x00, 0x6f, 0x74, 0x00,
 0x79, 0x7f, 0x00, 0x85, 0x8b, 0x00,
 0x91, 0x97, 0x00, 0x9d, 0xa3, 0x00,
 0xaa, 0xb0, 0x00, 0xb7, 0xbe, 0x00,
 0xc5, 0xcc, 0x00, 0xd3, 0xdb, 0x00,
 0xe2, 0xea, 0x00, 0xf2, 0xf9, 0x00,
 0x01, 0x0a, 0x11, 0x12, 0x1a, 0x11,
 0x23, 0x2b, 0x11, 0x34, 0x3d, 0x11,
 0x46, 0x4f, 0x11, 0x58, 0x62, 0x11,
 0x6b, 0x75, 0x11, 0x7f, 0x89, 0x11,
 0x93, 0x9d, 0x11, 0xa7, 0xb1, 0x11,
 0xbc, 0xc7, 0x11, 0xd1, 0xdc, 0x11,
 0xe7, 0xf2, 0x11, 0xfe, 0x09, 0x21,
 0x15, 0x20, 0x22, 0x2c, 0x38, 0x22,
 0x44, 0x50, 0x22, 0x5c, 0x69, 0x22,
 0x75, 0x82, 0x22, 0x8f, 0x9c, 0x22,
 0xa9, 0xb6, 0x22, 0xc3, 0xd1, 0x22,
 0xde, 0xec, 0x22, 0xfa, 0x07, 0x32,
 0x15, 0x24, 0x33, 0x32, 0x40, 0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 0xb3, 0xd1, 0xdd, 0xef, 0x0d, 0xed,
 0x2b, 0x49, 0xee, 0x68, 0x86, 0xee,
 0xa5, 0xc4, 0xee, 0xe3, 0x02, 0xfe,
 0x21, 0x40, 0xff, 0x5f, 0x7f, 0xff,
 0x9f, 0xbf, 0xff, 0xde, 0xff, 0xff,
 */
};


code BYTE* tAllFrontEndGammaTab[] =
{
  tFGamma,
};


code BYTE tAllFixGammaTab[] =

{
 0x00, 0x2f, 0x00, 0x45, 0x56, 0x00,
 0x65, 0x73, 0x00, 0x7f, 0x8a, 0x00,
 0x95, 0x9f, 0x00, 0xa9, 0xb2, 0x00,
 0xbb, 0xc3, 0x00, 0xcc, 0xd3, 0x00,
 0xdb, 0xe3, 0x00, 0xea, 0xf1, 0x00,
 0xf8, 0xff, 0x00, 0x06, 0x0c, 0x11,
 0x13, 0x19, 0x11, 0x1f, 0x25, 0x11,
 0x2b, 0x31, 0x11, 0x37, 0x3d, 0x11,
 0x42, 0x48, 0x11, 0x4d, 0x53, 0x11,
 0x58, 0x5e, 0x11, 0x63, 0x68, 0x11,
 0x6d, 0x72, 0x11, 0x77, 0x7c, 0x11,
 0x81, 0x86, 0x11, 0x8b, 0x8f, 0x11,
 0x94, 0x99, 0x11, 0x9d, 0xa2, 0x11,
 0xa6, 0xab, 0x11, 0xaf, 0xb4, 0x11,
 0xb8, 0xbd, 0x11, 0xc1, 0xc5, 0x11,
 0xc9, 0xce, 0x11, 0xd2, 0xd6, 0x11,
 0xda, 0xde, 0x11, 0xe2, 0xe6, 0x11,
 0xea, 0xee, 0x11, 0xf2, 0xf6, 0x11,
 0xfa, 0xfe, 0x11, 0x02, 0x06, 0x22,
 0x0a, 0x0d, 0x22, 0x11, 0x15, 0x22,
 0x19, 0x1c, 0x22, 0x20, 0x24, 0x22,
 0x28, 0x2b, 0x22, 0x2f, 0x32, 0x22,
 0x36, 0x3a, 0x22, 0x3d, 0x41, 0x22,
 0x44, 0x48, 0x22, 0x4b, 0x4f, 0x22,
 0x52, 0x55, 0x22, 0x59, 0x5c, 0x22,
 0x60, 0x63, 0x22, 0x66, 0x6a, 0x22,
 0x6d, 0x70, 0x22, 0x74, 0x77, 0x22,
 0x7a, 0x7d, 0x22, 0x81, 0x84, 0x22,
 0x87, 0x8a, 0x22, 0x8e, 0x91, 0x22,
 0x94, 0x97, 0x22, 0x9a, 0x9d, 0x22,
 0xa0, 0xa4, 0x22, 0xa7, 0xaa, 0x22,
 0xad, 0xb0, 0x22, 0xb3, 0xb6, 0x22,
 0xb9, 0xbc, 0x22, 0xbf, 0xc2, 0x22,
 0xc5, 0xc8, 0x22, 0xcb, 0xce, 0x22,
 0xd1, 0xd4, 0x22, 0xd7, 0xda, 0x22,
 0xdd, 0xe0, 0x22, 0xe2, 0xe5, 0x22,
 0xe8, 0xeb, 0x22, 0xee, 0xf1, 0x22,
 0xf4, 0xf6, 0x22, 0xf9, 0xfc, 0x22,
 0xff, 0x02, 0x32, 0x05, 0x07, 0x33,
 0x0a, 0x0d, 0x33, 0x10, 0x12, 0x33,
 0x15, 0x18, 0x33, 0x1b, 0x1d, 0x33,
 0x20, 0x23, 0x33, 0x25, 0x28, 0x33,
 0x2b, 0x2e, 0x33, 0x30, 0x33, 0x33,
 0x35, 0x38, 0x33, 0x3b, 0x3d, 0x33,
 0x40, 0x43, 0x33, 0x45, 0x48, 0x33,
 0x4b, 0x4d, 0x33, 0x50, 0x52, 0x33,
 0x55, 0x57, 0x33, 0x5a, 0x5d, 0x33,
 0x5f, 0x62, 0x33, 0x64, 0x67, 0x33,
 0x69, 0x6c, 0x33, 0x6e, 0x71, 0x33,
 0x73, 0x76, 0x33, 0x78, 0x7b, 0x33,
 0x7d, 0x80, 0x33, 0x82, 0x85, 0x33,
 0x87, 0x8a, 0x33, 0x8c, 0x8f, 0x33,
 0x91, 0x93, 0x33, 0x96, 0x98, 0x33,
 0x9b, 0x9d, 0x33, 0xa0, 0xa2, 0x33,
 0xa4, 0xa7, 0x33, 0xa9, 0xac, 0x33,
 0xae, 0xb0, 0x33, 0xb3, 0xb5, 0x33,
 0xb7, 0xba, 0x33, 0xbc, 0xbe, 0x33,
 0xc1, 0xc3, 0x33, 0xc6, 0xc8, 0x33,
 0xca, 0xcc, 0x33, 0xcf, 0xd1, 0x33,
 0xd3, 0xd6, 0x33, 0xd8, 0xda, 0x33,
 0xdd, 0xdf, 0x33, 0xe1, 0xe3, 0x33,
 0xe6, 0xe8, 0x33, 0xea, 0xed, 0x33,
 0xef, 0xf1, 0x33, 0xf3, 0xf6, 0x33,
 0xf8, 0xfa, 0x33, 0xfc, 0xff, 0x33,

 /* //2.2
 0x00, 0x40, 0x00, 0x5a, 0x6e, 0x00,
 0x80, 0x8f, 0x00, 0x9c, 0xa9, 0x00,
 0xb5, 0xc0, 0x00, 0xca, 0xd4, 0x00,
 0xdd, 0xe6, 0x00, 0xef, 0xf8, 0x00,
 0x00, 0x08, 0x11, 0x0f, 0x17, 0x11,
 0x1e, 0x25, 0x11, 0x2c, 0x33, 0x11,
 0x39, 0x40, 0x11, 0x46, 0x4c, 0x11,
 0x52, 0x58, 0x11, 0x5e, 0x64, 0x11,
 0x6a, 0x70, 0x11, 0x75, 0x7b, 0x11,
 0x80, 0x85, 0x11, 0x8a, 0x90, 0x11,
 0x95, 0x9a, 0x11, 0x9f, 0xa4, 0x11,
 0xa8, 0xad, 0x11, 0xb2, 0xb7, 0x11,
 0xbb, 0xc0, 0x11, 0xc4, 0xc9, 0x11,
 0xcd, 0xd2, 0x11, 0xd6, 0xdb, 0x11,
 0xdf, 0xe3, 0x11, 0xe7, 0xec, 0x11,
 0xf0, 0xf4, 0x11, 0xf8, 0xfc, 0x11,
 0x00, 0x04, 0x22, 0x08, 0x0c, 0x22,
 0x10, 0x14, 0x22, 0x17, 0x1b, 0x22,
 0x1f, 0x23, 0x22, 0x27, 0x2a, 0x22,
 0x2e, 0x32, 0x22, 0x35, 0x39, 0x22,
 0x3c, 0x40, 0x22, 0x44, 0x47, 0x22,
 0x4b, 0x4e, 0x22, 0x52, 0x55, 0x22,
 0x58, 0x5c, 0x22, 0x5f, 0x63, 0x22,
 0x66, 0x69, 0x22, 0x6d, 0x70, 0x22,
 0x73, 0x76, 0x22, 0x7a, 0x7d, 0x22,
 0x80, 0x83, 0x22, 0x87, 0x8a, 0x22,
 0x8d, 0x90, 0x22, 0x93, 0x96, 0x22,
 0x99, 0x9c, 0x22, 0x9f, 0xa2, 0x22,
 0xa5, 0xa8, 0x22, 0xac, 0xae, 0x22,
 0xb1, 0xb4, 0x22, 0xb7, 0xba, 0x22,
 0xbd, 0xc0, 0x22, 0xc3, 0xc6, 0x22,
 0xc9, 0xcc, 0x22, 0xcf, 0xd1, 0x22,
 0xd4, 0xd7, 0x22, 0xda, 0xdd, 0x22,
 0xe0, 0xe2, 0x22, 0xe5, 0xe8, 0x22,
 0xeb, 0xed, 0x22, 0xf0, 0xf3, 0x22,
 0xf6, 0xf8, 0x22, 0xfb, 0xfe, 0x22,
 0x00, 0x03, 0x33, 0x06, 0x08, 0x33,
 0x0b, 0x0d, 0x33, 0x10, 0x13, 0x33,
 0x15, 0x18, 0x33, 0x1a, 0x1d, 0x33,
 0x20, 0x22, 0x33, 0x25, 0x27, 0x33,
 0x2a, 0x2c, 0x33, 0x2f, 0x31, 0x33,
 0x34, 0x36, 0x33, 0x39, 0x3b, 0x33,
 0x3e, 0x40, 0x33, 0x43, 0x45, 0x33,
 0x48, 0x4a, 0x33, 0x4d, 0x4f, 0x33,
 0x51, 0x54, 0x33, 0x56, 0x59, 0x33,
 0x5b, 0x5d, 0x33, 0x60, 0x62, 0x33,
 0x64, 0x67, 0x33, 0x69, 0x6c, 0x33,
 0x6e, 0x70, 0x33, 0x73, 0x75, 0x33,
 0x77, 0x79, 0x33, 0x7c, 0x7e, 0x33,
 0x80, 0x83, 0x33, 0x85, 0x87, 0x33,
 0x89, 0x8c, 0x33, 0x8e, 0x90, 0x33,
 0x92, 0x95, 0x33, 0x97, 0x99, 0x33,
 0x9b, 0x9e, 0x33, 0xa0, 0xa2, 0x33,
 0xa4, 0xa6, 0x33, 0xa9, 0xab, 0x33,
 0xad, 0xaf, 0x33, 0xb1, 0xb4, 0x33,
 0xb6, 0xb8, 0x33, 0xba, 0xbc, 0x33,
 0xbe, 0xc0, 0x33, 0xc3, 0xc5, 0x33,
 0xc7, 0xc9, 0x33, 0xcb, 0xcd, 0x33,
 0xcf, 0xd1, 0x33, 0xd3, 0xd6, 0x33,
 0xd8, 0xda, 0x33, 0xdc, 0xde, 0x33,
 0xe0, 0xe2, 0x33, 0xe4, 0xe6, 0x33,
 0xe8, 0xea, 0x33, 0xec, 0xee, 0x33,
 0xf0, 0xf2, 0x33, 0xf4, 0xf6, 0x33,
 0xf8, 0xfa, 0x33, 0xfc, 0xff, 0x33,
 */
};

code BYTE* tAllxvYccFixGammaTab[] =
{
  tAllFixGammaTab,
};
#endif

void msWriteFGammaCM(BYTE bWindow,  BYTE ucGammaTableIdx)
{
    XDATA BYTE i,j;
    XDATA WORD ucAddr;
    XDATA short* pcTab1;
    XDATA short sTmp ;
    XDATA BYTE bUseExternal, idx;
    XDATA BYTE ucBank;
    ucBank = scReadByte( 0 );
    msWriteByte(SC0_00, 0x0E);

    if( bWindow == 0 )
    {
        ucAddr = SCE_04 ;
    }
    else
    {
        ucAddr = SCE_24;
    }

    bUseExternal = 1;//(ucGammaTableIdx>>7);
    idx =(ucGammaTableIdx&0xF) ;
    pcTab1 = (short *)tAllFGamma3x3Matrix[idx];

    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(bUseExternal==1)  // GET FROM EEPROM
            {
                {
                    sTmp = pcTab1[(i*3+j)];
                    scWriteByte( ucAddr, (sTmp>>8) &0xFF);
                    scWriteByte( ucAddr+1, sTmp&0xFF );
                    ucAddr += 2;
                }
            }
            else
            {
                sTmp = (WORD) pcTab1[i*3+j];
                if( sTmp >= 0 )
                {
                    if( sTmp > 0xfff )
                    {
                        sTmp = 0xfff;
                    }
                    msWrite2Byte( ucAddr-1, sTmp);//scWriteWord( ucAddr, sTmp );
                }
                else
                {
                    sTmp = sTmp *( -1);
                    if( sTmp > 0xfff )
                    {
                        sTmp = 0x1000;
                    }
                    sTmp = 0x2000 - sTmp ;
                    msWrite2Byte( ucAddr-1, sTmp);//scWriteWord( ucAddr, sTmp );

               }
               ucAddr += 2;
            } // if
        } // for
    } // for
	scWriteByte( 0, ucBank );
}

void msSetFGammaCMOnOff(BOOL xdata bWindow, BOOL xdata bSwitch)
{
	XDATA WORD reg;
	XDATA BYTE ucBank;
	ucBank = scReadByte( 0 );
    msWriteByte(SC0_00, 0x0E);
	if(bWindow)
		reg = SCE_22;
	else
		reg = SCE_02;

	msWriteBit(reg, bSwitch, _BIT1);
	scWriteByte( 0, ucBank );
}

void msSetFixGammaOnOff(BOOL bWindow, BOOL bSwitch)
{
	XDATA WORD reg;
	XDATA BYTE ucBank;
	ucBank = scReadByte( 0 );
    msWriteByte(SC0_00, 0x0E);
	if(bWindow)
		reg = SCE_22;
	else
		reg = SCE_02;

	msWriteBit(reg, bSwitch, _BIT2);
	scWriteByte( 0, ucBank );
}

void msSetFGammaOnOff(BOOL bWindow, BOOL bSwitch)
{
	XDATA BYTE ucBank;
	ucBank = scReadByte( 0 );
    msWriteByte(SC0_00, 0x0E);
	if(bWindow)
	    msWriteBit(SCE_22, bSwitch, _BIT0);
	else
	    msWriteBit(SCE_02, bSwitch, _BIT0);

	scWriteByte( 0, ucBank );
}

void msSetPostGammaOnOff(BOOL bWindow, BOOL bSwitch)
{
	XDATA BYTE ucBank;
	ucBank = scReadByte( 0 );
    msWriteByte(SC0_00, 0x08);
    if(bWindow)
        msWriteBit(SC8_3F, bSwitch, _BIT6);
    else
	    msWriteBit(SC8_3F, bSwitch, _BIT7);

	scWriteByte( 0, ucBank );
}

void msWriteFGammaTbl(BOOL bWindow, BYTE ModeIndex)
{
    XDATA BYTE ucTrigger,j ;
    //XDATA BYTE wLevel;
    XDATA WORD k, LasData;
    XDATA BYTE ucBank, *ucTab1;
    ucBank = scReadByte( 0 );
    msWriteByte(SC0_00, 0x0E);

    if(bWindow)
        msWriteBit(SCE_22, 0, _BIT0);
    else
        msWriteBit(SCE_02, 0, _BIT0);

	for(j = 0 ; j < 3; j++)
	{

		if(j == 0)      // R
		    ucTrigger = 0;
		else if(j == 1) // G
		    ucTrigger = _BIT2;
		else if(j == 2) // B
		    ucTrigger = _BIT3;
		else if(j == 3) // R/G/B
		    ucTrigger = _BIT2|_BIT3;

		msWrite2Byte(SCE_C0, 0x00);//LUT table R/W address
		msWriteByteMask(SCE_C2, _BIT1|_BIT0|ucTrigger , _BIT0|_BIT1|_BIT2|_BIT3); // wrtie enable, ise burst write
		msWriteByteMask(SCE_C2, 0 , BIT4);

        #if CHIP_ID == CHIP_TSUMF
            ucTab1 = tAllFrontEndGammaTab[ModeIndex];
            for( k = 0;k <256; k++ )
            {
                LasData = ucTab1[k*2] | (ucTab1[k*2+1]<<8) ;
                msWrite2Byte(SCE_C4, LasData);
            }
        #else
        	{
            XDATA WORD wData[2];
            XDATA BYTE i;
            ucTab1 = tAllFrontEndGammaTab[0];
            for( k = 0;k <384; k=k+3 )
            {
                wData[0] = ucTab1[k] | ((ucTab1[k+2]&0x0F)<<8) ;
                wData[1] = ucTab1[k+1] | ((ucTab1[k+2]&0xF0)<<4) ;

                for(i = 0; i < 2 ; i++)
                {
                    msWrite2Byte(SCE_C4, wData[i]);
                    LasData = wData[i];
                }
            }
        	}
        #endif

        for ( k = 0; k < 344; k++ ) //600 entry
            msWrite2Byte ( SCE_C4, LasData );

        LasData = 0;
	    msWriteByteMask(SCE_C2, 0x00, BIT1 | BIT0);	//msWriteByteMask(SC25_F0, 0x00, 0xC0);
	}

    scWriteByte( 0, ucBank );
}

void msWriteFixGammaTbl(BOOL bWindow, BYTE ModeIndex)
{
    XDATA BYTE ucTrigger,j,i ;
    XDATA WORD wData[2],k;
    XDATA BYTE ucBank, *ucTab1;
    ucBank = scReadByte( 0 );
    msWriteByte(SC0_00, 0x0E);
    if(bWindow)
        msWriteBit(SCE_22, 0, _BIT2);
    else
        msWriteBit(SCE_02, 0, _BIT2);

	for(j = 0 ; j < 3; j++)
	{
	    #if CHIP_ID == CHIP_TSUMF
            ucTab1 = tAllxvYccFixGammaTab[ModeIndex];
        #else
		    ucTab1 = tAllxvYccFixGammaTab[0];
        #endif
		if(j == 0)      // R
		    ucTrigger = 0;
		else if(j == 1) // G
		    ucTrigger = _BIT2;
		else if(j == 2) // B
		    ucTrigger = _BIT3;
		else if(j == 3) // R/G/B
		    ucTrigger = _BIT2|_BIT3;

		msWrite2Byte(SCE_E0, 0x00);//LUT table R/W address
		msWriteByteMask(SCE_E2, _BIT1|_BIT0|ucTrigger , _BIT0|_BIT1|_BIT2|_BIT3); // wrtie enable, ise burst write
		msWriteByteMask(SCE_E2, 0 , BIT4);

        for( k = 0;k <384; k=k+3 )
        {
            wData[0] = ucTab1[k] | ((ucTab1[k+2]&0x0F)<<8) ;
            wData[1] = ucTab1[k+1] | ((ucTab1[k+2]&0xF0)<<4) ;

            for(i = 0; i < 2 ; i++)
            {
                msWrite2Byte(SCE_E4, wData[i]);
            }
        }
	    msWriteByteMask(SCE_E2, 0x00, BIT1 | BIT0);	//msWriteByteMask(SC25_F0, 0x00, 0xC0);
	}
    scWriteByte( 0, ucBank );
}

void LoadPostGammaTbl(BOOL bWindow, BYTE GamIndex)
{
	XDATA WORD i, BaseAddr,  GammaTbl[4], DiffValue0, DiffValue1 ,SumValue;
	XDATA BYTE NvrValue, j , ucTrigger, ChannelIndex = 0 , ucRGBGamAddStart = 0 ;
	XDATA BYTE ucBank;

    BaseAddr = BGammaTblAddr+ (GammaTblSize*GamIndex);//BGammaTblAddr1;

	ucBank = scReadByte( 0 );
    msWriteByte(SC0_00, 0x08);
	msWrite2Byte(SC8_20, 0x00);//msWriteWord( SC8_21, 0x00 );
	msWriteByteMask( SC8_22, 0x03,BIT1|BIT0);// Main
	bWindow = bWindow ;

	for(ChannelIndex= 0 ; ChannelIndex < 3;ChannelIndex++)
	{
		if(ChannelIndex == 0) // R
		{
			ucTrigger = 0;
			//printData("  ===== R ucRGBGamAddStart ======= %x ", (GammaTblSize * ChannelIndex));
		}
		else if(ChannelIndex == 1) // G
		{
			ucTrigger = _BIT2;
			//printData("  ===== G ucRGBGamAddStart ======= %x ", (GammaTblSize * ChannelIndex));
		}
		else if(ChannelIndex == 2) // B
		{
			ucTrigger = _BIT3;
			//printData("  ===== B ucRGBGamAddStart ======= %x ", (GammaTblSize * ChannelIndex));
		}

		ucRGBGamAddStart = GammaTblSize * ChannelIndex ;
		msWriteByteMask( SC8_22, ucTrigger , BIT2|BIT3);

		for(j=0; j<4; j++)
            GammaTbl[j] = 0;

		SumValue = 0;

		for (i=0; i < GammaTblSize; i++)
		{
            #if USEFLASH
                NvrValue = FlashReadByte(BaseAddr+ucRGBGamAddStart+i);
            #else
                NVRam_ReadByte(BaseAddr+ucRGBGamAddStart+i, &NvrValue);//NVRam_ReadByte(BaseAddr+ucRGBGamAddStart+i, &NvrValue);
            #endif

            //printData(" ucEEPGmaTBLBuf[%x]", NvrValue);
            DiffValue1 = NvrValue & 0x0F;
            DiffValue0 = (NvrValue & 0xF0) >>4;

            if(i==0)
            {
                GammaTbl[0] = 0;
            }
            else
            {
                GammaTbl[0] = GammaTbl[3] ;
                GammaTbl[0] = GammaTbl[0] + DiffValue0 ;
            }

            GammaTbl[1] = GammaTbl[0] + DiffValue0 ;
            GammaTbl[2] = GammaTbl[1] + DiffValue1 ;
            GammaTbl[3] = GammaTbl[2] + DiffValue1 ;

            for(j=0; j<4; j++)
            {
                if(GammaTbl[j] > 1023)
                    GammaTbl[j] = 1023;
                //printData(" GammaTbl=  %x", GammaTbl[j]);
                msWrite2Byte(SC8_24, GammaTbl[j]);
            }
		}

	}
	msWriteByteMask( SC8_22, 0, BIT1 | BIT0 );
	scWriteByte( 0, ucBank );
}

void LoadDeltaEColorMode(BOOL bWindow, BYTE ModeIndex)
{
    //DeGamma
    msWriteFGammaTbl(bWindow,ModeIndex);
    msSetFGammaOnOff(bWindow,1);
    //Color Matrix
    #if USEFLASH
        Flash_ReadTbl(RM_COLORSPACE_TRANS_BASEADDR, tAllFGamma3x3Matrix[0], ColorMatrixSize*2);
    #else
        NVRam_ReadTbl(RM_COLORSPACE_TRANS_BASEADDR, tAllFGamma3x3Matrix[0], ColorMatrixSize*2);
    #endif

    msWriteFGammaCM(bWindow,ModeIndex);
    msSetFGammaCMOnOff(bWindow,1);
    //FixGamma
    msWriteFixGammaTbl(bWindow,ModeIndex);
    msSetFixGammaOnOff(bWindow,1);
    //Post Gamma
    LoadPostGammaTbl(bWindow,0);
    msSetPostGammaOnOff(bWindow,1);
}

#endif

